public class Main {
    public static void main(String[] args) {
        int[] arr = {16, 18, 5, 3, 4, 29, 10, 9, 25, 21, 20};
        //insertSort(arr);
        //display(arr);
        //bubbleSort(arr);
        //display(arr);
        //quickSort(arr, 0, arr.length - 1);
        //display(arr);
        selectSort(arr);
        display(arr);

    }


    //插入排序
    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int temp = arr[i];
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j + 1] < arr[j]) {
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                } else if (arr[j + 1] > arr[j]) {
                    break;
                }
                if (j == 0) {
                    arr[0] = temp;
                }
            }
            //display(arr);

        }
    }


    //冒泡排序
    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1 - i; i++) {
            for (int j = 0; j < arr.length - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            //display(arr);
        }
    }


    //快速排序
    private static void quickSort(int[] arr, int low, int high) {
        if (low > high) {
            return;
        }
        int i = low;
        int j = high;
        int temp = arr[low];
        while (i < j) {
            //比较
            while (i < j && arr[j] > temp) {
                j--;
            }
            //交换
            if (i < j) {
                int p = arr[i];
                arr[i] = arr[j];
                arr[j] = p;
            }
            //比较
            while (i < j && arr[i] <= temp) {
                i++;
            }
            //交换
            if (i < j) {
                int p = arr[i];
                arr[i] = arr[j];
                arr[j] = p;
            }
            //display(arr);
        }

        //递归
        quickSort(arr, low, i - 1);
        quickSort(arr, i + 1, high);

    }


    //选择排序
    private static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int min = arr[i];
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (min > arr[j]) {
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
            display(arr);
        }
    }


    //打印
    private static void display(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
        System.out.println();
    }
}
